package org.monkey.example.request.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.stream.Collectors;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.monkey.common.cbs.util.PageConvertUtil;
import org.monkey.common.constant.entity.Result;
import org.monkey.common.constant.util.DataUtil;
import org.monkey.example.request.entity.convert.RequestInfoConvert;
import org.monkey.example.request.entity.dto.RequestInfoDTO;
import org.monkey.example.request.entity.form.RequestInfoAddForm;
import org.monkey.example.request.entity.form.RequestInfoListForm;
import org.monkey.example.request.entity.form.RequestInfoPageForm;
import org.monkey.example.request.entity.form.RequestInfoSaveForm;
import org.monkey.example.request.entity.form.RequestInfoUpdateForm;
import org.monkey.example.request.entity.vo.RequestInfoVO;
import org.monkey.example.request.service.RequestInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@Data
@Api(value = "请求日志", tags = "请求日志")
@RestController
@ResponseBody
@RequestMapping("/v1/requestInfo")
public class RequestInfoController {


  @Autowired
  RequestInfoService requestInfoService;
  @Autowired
  RequestInfoConvert requestInfoConvert;
  @ApiOperation(value = "id查询", notes = "id查询")
  @GetMapping
  public Result<RequestInfoVO> get(@NotNull(message = "id必填") Long id,@RequestParam(required = false,defaultValue = "false") Boolean nullThrow) {
    RequestInfoDTO dto = requestInfoService.getById(id,nullThrow);
    return Result.success(requestInfoConvert.dto2Vo(dto));
  }
  @ApiOperation(value = "删除", notes = "删除")
  @PostMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_VALUE,consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
  public Result<Boolean> delete(@NotNull(message = "id必填") Long id) {
    return Result.success(requestInfoService.delete(id));
  }
  @ApiOperation(value = "添加", notes = "添加")
  @PostMapping(value = "/add",produces = MediaType.APPLICATION_JSON_VALUE,consumes = MediaType.APPLICATION_JSON_VALUE)
  public Result<RequestInfoVO> add(@RequestBody @Valid RequestInfoAddForm addForm) {
    RequestInfoDTO  dto = requestInfoService.add(addForm);
    return Result.success(requestInfoConvert.dto2Vo(dto));
  }
  @ApiOperation(value = "更新", notes = "更新")
  @PostMapping("/update")
  public Result<RequestInfoVO> update(@RequestBody  @Valid RequestInfoUpdateForm updateForm) {
    RequestInfoDTO  dto = requestInfoService.update(updateForm);
    return Result.success(requestInfoConvert.dto2Vo(dto));
  }
  @ApiOperation(value = "添加或更新", notes = "添加或更新")
  @PostMapping("/save")
  public Result<RequestInfoVO> save(@RequestBody  @Valid RequestInfoSaveForm saveForm) {
    RequestInfoDTO  dto = requestInfoService.save(saveForm);
    return Result.success(requestInfoConvert.dto2Vo(dto));
  }
  @ApiOperation(value = "获取所有", notes = "获取所有")
  @GetMapping("/listAll")
  public Result<List<RequestInfoVO>> listAll() {
    List<RequestInfoDTO> dts = requestInfoService.listAll();
    return Result.success(DataUtil.colTypeChange(dts,dto->requestInfoConvert.dto2Vo(dto)));
  }
  @ApiOperation(value = "分页条件查询", notes = "分页条件查询")
  @PostMapping(value = "/page",produces = MediaType.APPLICATION_JSON_VALUE,consumes = MediaType.APPLICATION_JSON_VALUE)
  public Result<IPage<RequestInfoVO>> page(@RequestBody  @Valid RequestInfoPageForm pageForm){
    IPage<RequestInfoDTO> generateDtPage = requestInfoService.page(pageForm);
    return Result.success(PageConvertUtil.convert(generateDtPage,g->requestInfoConvert.dto2Vo(g)));
  }
  @ApiOperation(value = "条件查询", notes = "条件查询")
  @PostMapping(value = "/list",produces = MediaType.APPLICATION_JSON_VALUE,consumes = MediaType.APPLICATION_JSON_VALUE)
  public Result<List<RequestInfoVO>> list(@RequestBody  @Valid RequestInfoListForm listForm){
    List<RequestInfoDTO> dts = requestInfoService.list(listForm);
    return Result.success(dts.stream().map(g->requestInfoConvert.dto2Vo(g)).collect(Collectors.toList()));
  }
}
